import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { HttpService } from '../../shared/service/http.service';

@Component({
  selector: 'app-article-list',
  templateUrl: './article-list.component.html',
  styleUrls: ['./article-list.component.scss'],
})
export class ArticleListComponent implements OnInit {
  @Input() tagData: any;
  pageTotal: 0;
  pageSize: 0;
  pageNo: any = 1;
  params: any;
  constructor(
    private appGlobalService: AppGlobalService,
    private http: HttpService
  ) {
  }
  ngOnInit() {
    this.params = this.tagData.data.submit;
    this.pageNo = this.tagData.data.pageNo;
    this.pageSize = this.tagData.data.pageSize;
    this.pageTotal = this.tagData.data.pageTotal;
  }
  get(obj) {
    const params = {
      pageId: obj.target,
      params: obj.params,
      sessionId: localStorage.getItem('sessionId')
    };
    this.appGlobalService.goToForward(params);
  }

  loadMoreData(event) {
    setTimeout(() => {
      this.params.sessionId = localStorage.getItem('sessionId');
      // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      this.params.content.pageNo = this.pageNo;
      // 请求获取新数据
      this.http.post('application/submit', this.params, res => {
        // 请求数据成功
        if (res.success) {
          // 追加返回数据
          if (Array.isArray(res.data.data) && res.data.data.length > 0) {
            // 追加数据
            res.data.data.map(item => {
              this.tagData.data.data.push(item);
            });
            // this.params = res.data.submit;
          } else {
            // 没有数据了
            // this.appGlobalService.showToast('没有数据了...', 1000, 'bottom');
          }
          event.target.complete();
        } else {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(res.message, 2000, 'middle');
          event.target.complete();
        }
      });
    }, 500);
  }

  getNgStyle(str) {
    try {
      return JSON.parse(str);
    } catch (error) {
      return {};
    }
  }
}
